package com.java2007.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * Jdbc工具类
 *
 * @author ghy
 * @version 1.0
 * @date 2020-12-29
 */
public class JdbcUtil {
    //配置文件对象
    private static final Properties PROPERTIES = new Properties();
    //获取线程对象
    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
    //连接对象设置空值
    private static Connection connection = null;
    //数据源对象设置空值
    private static DruidDataSource dataSource = null;

    static {
        InputStream in = null;
        try {
            //获取配置文件流对象
            in = JdbcUtil.class.getClassLoader().getResourceAsStream("druid.properties");
            //通过流对象加载配置文件
            PROPERTIES.load(in);
            //Druid工厂反射创建数据源对象
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(PROPERTIES);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 返回数据源
     * @return
     */
    public static DataSource getDataSource() {
        return dataSource;
    }

//    public static void main(String[] args) {
//        DataSource dataSource = getDataSource();
//        System.out.println(dataSource);
//    }

    /**
     * 获取连接
     *
     * @return
     */
    public static Connection getConnection() {
        try {

            //先从 ThreadLocal上获取绑定的线程
            connection = threadLocal.get();
            if (null == connection) {
                //如果线程上没有connection，获取一个
                connection = dataSource.getConnection();
                //把connection对象绑定到本地线程上
                threadLocal.set(connection);
            }
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 释放资源
     *
     * @param resultSet
     * @param statement
     * @param connection
     */
    public static void release(ResultSet resultSet, Statement statement, Connection connection) {
        try {
            if (null != resultSet) {
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (null != statement) {
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (null != connection) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 开启事务
     */
    public static void begin() {
        try {
            JdbcUtil.getConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 提交事务
     */
    public static void commit() {
        try {
            JdbcUtil.getConnection().commit();
            //从线程上解绑
            threadLocal.remove();
            //释放资源
            JdbcUtil.release(null, null,connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 回滚事务
     */
    public static void rollback() {
        try {
            JdbcUtil.getConnection().rollback();
            //从线程上解绑
            threadLocal.remove();
            //释放资源
            JdbcUtil.release(null, null,connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


//    public static void main(String[] args) {
//        Connection connection = getConnection();
//        System.out.println(connection);
//    }

}
